package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/7/10 0010.
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Role role) {
        if(role.getId() == null){
            roleMapper.insert(role);
        }else{
            roleMapper.updateByPrimaryKey(role);
        }
    }




    /**
     * 分页的 业务
     * @param queryObject   分页的参数
     * @return
     */
    @Override
    public PageResult query(QueryObject queryObject) {
        // 1 如果数据库中表没有数据那么需要分页吗 , 肯不不需要分页
        // count 就是数据库有多少条数据
       int count =  roleMapper.selectForCount(queryObject);
       if(count == 0){
           return PageResult.DEFAULT_DATA;
       }
        // 2 如果表中有数据 那么需要分页..
        List<Role>  list = roleMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }

}
